/**
 * 工具类
 */
(function () {
    if (!window.PM) {
        window.PM = {};
    }
    PM = {
      fnRequestAjax: function (url, data, type, successfn, errorfn,completefn) {
        type = (type == null || type == "" || typeof(type) == "undefined") ? "post" : type;
        data = (data == null || data == "" || typeof(data) == "undefined") ? "" : data;
        if (type !== "get") {
          if ($.isPlainObject(data)) {
            data = JSON.stringify(data);
          }
        }
        var strToken = "";
        if (getCookie("token")) {
          strToken = "Bearer " + getCookie("token");
        }
        $.ajax({
          type: type,
          async: true,
          data: data,
          url: url,
          crossDomain: true,
          contentType: 'application/json;charset=utf-8',
          dataType: 'json',
          xhrFields: {
            withCredentials: false
          },
          headers: {
            'Authorization': strToken
          },
          success: function (response) {
            if ( response.status_code ) {//正常情况没有这玩意,不知道为何留着。
              $.warningModal({
                type: 'alert',
                content: response.message
              });
            } else {
              successfn(response);
            }
          },
          error: function (response) {
            try {
              var data = JSON.parse(response.responseText);
              if (data.status_code === 422) {
                for (var error in data['errors']) {
                  var message = data['errors'][error][0];
                  break;
                };
                $.warningModal({
                  type:'alert',
                  content:message,
                  confirm:function(){}
                });
              } else {
                var message = data['message'];
                console.log("---------------------"+data.status_code);
                if(data.status_code === 401){
                  if(window.location.pathname !== "/"){
                    $.warningModal({
                      type:'alert',
                      content:message,
                      confirm:function(){
                        window.location.href = "/";
                      }
                    });
                  }
                  PM.setCookie("token","",30);
                  PM.setCookie("name","",30);
                }else{
                  $.warningModal({
                    type: 'alert',
                    content: message,
                    confirm:function(){}
                  });
                }
              }

            } catch (e) {
              console.log("error=" + e);
              $.warningModal({
                type:'alert',
                content: '服务器开小差了，请稍后重试',
                confirm:function(){}
              });
            }
            console.log("error",response )
            errorfn(response);
          },
          complete: function (response) {
            if (response.status) {
              completefn(response);
            }
          }
        });
      },
    };
})(window);

function getCookie(cookiename) {
  var nameEQ = cookiename + "=";
  var ca = document.cookie.split(';');    //把cookie分割成组
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];                      //取得字符串
    while (c.charAt(0) == ' ') {          //判断一下字符串有没有前导空格
      c = c.substring(1, c.length);      //有的话，从第二位开始取
    }
    if (c.indexOf(nameEQ) == 0) {       //如果含有我们要的name
      return unescape(c.substring(nameEQ.length, c.length));    //解码并截取我们要值
    }
  }
  return false;
}
